<template>
	<gui-page ref="guiPage" :customHeader="true">
		<!-- 导航 -->
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :leftType="'1'" :title="$t('locationBarcodeInfo.scrapMark')"
				:rightText="$t('distributedEnter.scanRecord')" @clickRight="clickRight"
				@clickLeftBack="clickLeftBack"></nav-bar-custom>
		</template>

		<template v-slot:gBody>
			<view class="barcodeSplitting">
				<!-- 扫描条码部分 -->
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="custom-card-title">
						<text class="title-line"></text>
						{{ $t('Common.scanningInfo') }}
					</view>
					<view class="gui-form-item gui-border-b" style="padding: 20rpx 0;">
						<text class="item-info-title" style="padding-right: 40rpx;">
							<text class="required-color">*</text>
							{{ $t('distributedEnter.barcodeScan') }}
						</text>
						<uni-easyinput class="uni-mt-5" suffixIcon="scan" v-model="data.barCodes" type="search"
							:placeholder="$t('Common.scanningBarCode')" @confirm="confirm" @iconClick="iconClick">
						</uni-easyinput>
					</view>
				</view>

				<!-- 提示信息 -->
				<view class="gui-flex gui-rows gui-nowrap gui-align-items-center gui-margin-top">
					<view class="gui-title-line"></view>
					<text class="gui-primary-text gui-h6" style="padding-left:50rpx; padding-right:50rpx;">{{
						$t('hardwareOut.barcodeInformation') }}</text>
					<view class="gui-title-line"></view>
				</view>

				<!-- 条码展示部分 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll">
					<view style="height: 15rpx"></view>
					<view class="card-boxCustom" v-if="data.cardShow">
						<view class="card-body">
							<view class="custom-card-title">
								<text class="title-line"></text>{{ $t('distributedOut.atPresent') }}
							</view>
							<view class="custom-card-itemOne">
								<text class="item-info-title">{{ $t('distributedOut.barcode') }}：</text>{{
									data.scanInfo.carton_no }}
							</view>
							<!-- <view class="custom-card-itemOne">
								<text class="body-item-title">{{
									$t('multilingual.productionLine') }}：</text>{{ data.scanInfo.productionLineCode }}
							</view> -->
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedEnter.materialCode')
								}}：</text>{{ data.scanInfo.material_code }}
							</view>
							
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{
									$t('locationBarcodeInfo.positions') }}：</text>{{ data.scanInfo.location }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('translationImage.productionTime')
								}}：</text>{{ data.scanInfo.output_time }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedEnter.qty') }}：</text>{{
									data.scanInfo.qty
								}}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{
									$t('multilingual.state') }}：</text>{{ data.scanInfo.status }}
							</view>
							<!-- <view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedEnter.uom') }}：</text>{{ data.scanInfo.unit
								}}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedOut.warehouse')
								}}：</text>{{ data.scanInfo.warehouseCode }}
							</view>
							<view class="custom-card-itemOne">
								<text class="body-item-title">{{ $t('distributedOut.location')
								}}：</text>{{ data.scanInfo.locationCode }}
							</view> -->
						</view>
					</view>
				</scroll-view>

				<!-- 底部确定按钮 -->
				<view class="page-bottom-box bottom-postion">
					<view class="page-bottomBtn bottomBtn-bgcolor-green" @click="submit">{{ $t('calendar.submit') }}</view>
				</view>

				<!-- 普通弹窗 -->
				<uni-popup ref="popup" type="center">
					<view class="input-dialog-box" style="height:400rpx">
						<view class="input-dialog-content" style="margin-bottom: 120rpx;">
							<view class="custom-card-itemTwo" style="padding: 20rpx 0;">
								<text class="required-color">*</text>
								<text class="gui-form-label gui-secondary-text">{{ $t('locationBarcodeInfo.waste')
								}}</text>
								<view class="gui-form-body gui-text-right" style="overflow:visible;">
									<superweiCombox style="text-align:right;" :candidates="data.selectList"
										:placeholder="$t('distributedOut.placeholder')" :isJSON="true" keyName="name"
										@select="select" v-model="data.selectIdText">
									</superweiCombox>
								</view>
							</view>
						</view>
						<view class="input-dialog-footer">
							<view class="input-dialog-button" @click="cancelFn">
								{{ $t('popup.cancel') }}
							</view>
							<view class="input-dialog-button dialog-button-left" @click="submitConfirmFn">
								{{ $t('popup.confirm') }}
							</view>
						</view>
					</view>
				</uni-popup>
			</view>
		</template>
	</gui-page>
</template>

<script setup>
//引入文件
import { navigate, navigateBack } from '@/utils/routerSkip.js';
import { ref, reactive, onMounted, computed, watch, toRaw } from "vue";
import { wmsSelectWmsBarcodeInfo, modifyTrayStatusInRestArea ,getRestareaInfoScan} from '@/api/api.js'
import pdaScan from '@/hooks/pdaScan.js'
import storageBin from '@/components/storageBin.vue'
import comboGrid from '@/components/comboGrid.vue'
import { onBackPress } from '@dcloudio/uni-app';
import { useStore } from "vuex";
import { basPdaOperationLogFn } from '@/utils/index.js';
import superweiCombox from '@/components/superweiCombox';
import { USERINFO, OPERATION_COMMIT, OPERATION_LOOK, OPERATION_SCAN } from '@/constant/index.js'

//JS国际化引入
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

//注册组件
components: { comboGrid }

let store = useStore();
const { scan } = pdaScan();

// 弹框的属性
let popup = ref(null);

const data = reactive({
	barCodes: '',  // 条码
	barcodeList: [],// 扫描记录的存储
	scanInfo: {}, //当前扫描信息
	scanInfoItem: {}, //产线或者物料选择的数据存储
	selectList: [{
		id: 'OK',
		name: t('index.yes')
		
	}, {
		id: 'NG',	
		name: t('index.no')
	}],
	selectIdText: '', // 是否废品
	selectId: ''
})

//
onMounted(() => {
	uni.$on('scancodedate', (data) => {
		// console.log('获取到的code：' + data.code)
		getCode(data.code, 'pda')
	})
})


// 存储扫描数据到vuex
data.barcodeList = computed(() => {
	return store.state.scrapMarkList
})

// 扫描条码事件
const confirm = () => {
	getCode(data.barCodes, 'keydown')
}

// 扫描条码  监听用户使用相机
const iconClick = async (type) => {
	if (type === 'suffix') {
		let code = await scan();
		getCode(code, 'scan')
	}
}

const getCode = (code, type) => {
	if (code.length <= 0) {
		uni.showToast({ title: t('newTranslation.correctly'), icon: 'none' });
		return
	}
	// 存储扫描的的条码,通过barode去判断是否有重复
	let arr = data.barcodeList.filter(x => x.barcode == code);

	if (arr.length > 0) {
		uni.showToast({ title: t('smashRemoval.codesRepeatedly'), icon: 'none' });
		return
	}
	data.barCodes = code;
	let parmes = {
		isSp: 0,
		barcode: code,
		selectType: 'barcodeManage'
	}
	uni.showLoading({
		title: 'loading',
		mask: true
	});
	// 调用扫描的接口
	getRestareaInfoScan(parmes).then(res => {
		uni.hideLoading();
		try {
			let inputParameter = '无'
			let returnParameter = JSON.stringify(data.barcodeList)
			let operationPicture = '废品标记'
			let callInterface = '无'
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: '无', //返回状态
				errorMessage: '无', //错误信息
				isPass: 'Y', //是否成功 Y N
				operationType: OPERATION_SCAN
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
			// console.log(error)
		}
		if (res.Status == 200) {
			if(res.LstData.length>0){
				uni.showToast({ title: t('locationBarcodeInfo.successfullyScan'), icon: "none" });
				data.scanInfo = res.LstData[0]
				data.cardShow = true
				// 数据存储到vuex
				store.commit('addScrapMark', data.scanInfo)
			}else{
				uni.showToast({ title: t('Common.scanerror'), icon: "none" });
			}
			
		}
		else {
			uni.showToast({ title: res.Message, icon: "none" });
			data.cardShow = false
		}
	})

}

// 提交
const submit = () => {
	// console.log(data.barcodeList)
	if (!data.barcodeList.length) {  //判断请扫描条码
		uni.showToast({
			title: t('stockManage.pleaseScanCode'),
			icon: 'none'
		})
		return
	}
    data.selectIdText=''
	// 打开弹框
	popup.value.open();
	
}


// 校验状态的选择事件
const select = (e) => {
	data.selectId = e.id;
}

// 提交弹框确定
const submitConfirmFn = () => {
	let barcode = []
	data.barcodeList.forEach(itme => {
		barcode.push(itme.carton_no)
	})
	// 判断是否废板选择
	if (data.selectIdText.length) {
		let parmes = {
			lstBarcode: barcode,
			status:( data.selectIdText=='是'||data.selectIdText=='Yes')?"NG":"OK"
		}
		modifyTrayStatusInRestArea(parmes).then(res => {
			// 接口调用成功执行
			if (res.Status == 200) {
				// 清空数据
				data.scanInfo = {};
				data.cardShow = false
				popup.value.close()
				store.commit('clearScrapMark')
				uni.showToast({ title: t('smashRemoval.submitSuccessfully'), icon: "none" });

			}
			else {
				uni.showModal({
					content: res.Message,
					showCancel:false,
					confirmText:'close'
				})
				
			}
		})

	} else {
		uni.showToast({ title: t('locationBarcodeInfo.wastePlease'), icon: 'none' });
		return
	}
}

// 弹框的取消
const cancelFn = () => {
	popup.value.close()
	// 清空扫描条码
	data.barCodes = ''
}

//扫描纪录
const clickRight = () => {
	navigate({
		url: '/pages/scrapMark/scrapMarkRecord'
	})
}

//点击物理按键的返回
onBackPress((options) => {
	if (options.from == 'backbutton') {
		if (!data.barcodeList.length) {
			uni.navigateBack({
				delta: 1
			});
			return true
		}
		uni.showModal({
			title: t('smashRemoval.warmPrompt'),
			content: t('materials.unsubmittedDataIn'),
			success: function (res) {
				if (res.confirm) {
					//清除store 里面的数据
					store.commit("clearScrapMark");
					uni.navigateBack({
						delta: 1
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
		return true;
	}
})

//导航栏左侧返回
const clickLeftBack = () => {
	navBackCheck()
}
const navBackCheck = () => {
	if (!data.barcodeList.length) {
		uni.navigateBack({
			delta: 1
		});
		return true
	}
	uni.showModal({
		title: t('smashRemoval.warmPrompt'),
		content: t('materials.unsubmittedDataIn'),
		success: function (res) {
			if (res.confirm) {
				store.commit('clearScrapMark') // 清空条码数据
				uni.navigateBack({
					delta: 1
				});
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});
}
</script>

<style lang="scss" scoped>
.barcodeSplitting {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}
</style>
